const EventEmitter = require('../../common/EventEmitter');
const config = require('../../common/config');
const requester = require('../../common/requester');
const $$onLoad = require('../../appLoaded/onLoad');
const app = getApp();

Page({
    data: {
        categories: [''],
        categoryIndex: 0,
        videoSource: [],
        isAudit: true,
        isAuthed: true
    },

    onLoad: function () {
        this.pageIndex = 1;
        $$onLoad()
            .then(() => {
                let isAudit = app.globalData.isAudit,
                    isAuthed = app.globalData.isAuthed;

                this.setData({
                    isAudit,
                    isAuthed
                });

                if (isAuthed) {
                    this.init();
                }
            });
    },

    init: function () {
        this.setData({
            isAuthed: true
        });
        this.queryIndexCategory()
            .then(this.queryVideosByPage)
            .then(res => {
                console.log('init success');
            });
        EventEmitter.addListener('backFormComment', (id) => {
            this.updateLikeStatus(id);
        });
    },

    /**
     * 下拉刷新
     */
    onPullDownRefresh: function () {
        console.log("onPullDownRefresh.....");
        this.isLoadMore = true;
        this.pageIndex = 1;
        this.queryVideosByPage()
            .then(res => {
                this.isLoadMore = false;
            });
    },

    /***
     * 下拉刷新停止
     */
    onPullDownStop: function () {
        console.log("onPullDownStop.....");
        wx.stopPullDownRefresh();
    },

    onVideoCellTap: function (e) {
        let id = e.currentTarget.dataset.id;
        wx.navigateTo({
            url: '../videoDetail/videoDetail?' + 'id=' + id
        });
    },

    onCategoryItemTap: function (e) {
        let index = e.currentTarget.dataset.index;
        this.isLoadMore = true;
        this.pageIndexHandler('reset');
        this.setFrontCategory(index);
        this.queryVideosByPage()
            .then(res => {
                this.isLoadMore = false;
            });
    },

    updateLikeStatus: function (id) {
        let videoSource = this.data.videoSource;
        videoSource = videoSource.map(video => {
            // if(id===video.resourceId){
            //     let
            //     return video.isLiked=  video.isLiked
            // }
        })
    },

    queryIndexCategory: function () {
        return requester.request({
            url: requester.service.queryIndexCategory,
            data: {}
        }).then(res => {
            this.setData({
                categories: res.data || ['']
            })
        })
    },

    queryVideosByPage: function () {
        if (this.pageIndex !== 1) {
            wx.showLoading({
                title:'加载中...'
            });
        }
        let {
            categories,
            categoryIndex,
            videoSource
        } = this.data;

        let data = {
            indexCategory: categories[categoryIndex],
            pageIndex: this.pageIndex
        };
        return requester.request({
            url: requester.service.queryVideosByPage,
            data: data
        }).then(res => {
            let video = res.data.simpleVideoResourceVos;

            this.setData({
                videoSource: this.isLoadMore
                    ? video
                    : videoSource.concat(video)
            }, () => {
                wx.hideLoading();
            });

            this.onPullDownStop();
        });
    },

    pageIndexHandler: function (type) {
        /**
         * 切换 tab
         */
        const reset = () => {
            this.pageIndex = 1
        };
        /**
         * 加载更多
         */
        const add = () => {
            this.pageIndex++
        };

        const action = {
            'reset': reset,
            'add': add
        };

        action[type]();
    },

    setFrontCategory: function (index) {
        this.setData({
            categoryIndex: index
        })
    },

    /***
     * 滑动到底部，加载更多
     */
    onReachBottom: function () {
        console.log("onReachBottom.....");
        this.pageIndexHandler('add');
        this.queryVideosByPage();
    },

    //跳转到世界视频小程序
    jumpMainApp: function () {
        wx.navigateToMiniProgram({
            appId: 'wxaf61541f1b310835'
        })
    },

    onShareAppMessage: function (res) {
        return {
            title: '用小画廊，看不一样的精彩',
            path: '/pages/videoList/videoList',
            success: function (res) {
                // 转发成功
                wx.showToast({
                    title: '分享成功',
                    icon: 'success',
                    duration: 1000
                })
            },
            fail: function (res) {
                // 转发失败
                wx.showToast({
                    title: '分享失败',
                    icon: 'none',
                    duration: 1000
                })
            }
        }
    }
});
